package com.czxy.enterprise.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czxy.domain.TbEnterprise;
import com.czxy.domain.TbUser;
import com.czxy.enterprise.config.GoEasyPublish;
import com.czxy.enterprise.config.JwtProperties;
import com.czxy.enterprise.service.EnterpriseService;
import com.czxy.findjob.utils.JwtUtils;
import com.czxy.findjob.utils.RsaUtils;
import com.czxy.findjob.vo.BaseResult;
import com.czxy.vo.EnterpriseVo;
import io.swagger.annotations.ApiModelProperty;
import net.sf.jsqlparser.statement.select.Wait;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 企业表 前端控制器
 * </p>
 *
 * @author xuewen
 * @since 2022-01-13
 */
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;
    @Resource
    private GoEasyPublish goEasyPublish;
    @GetMapping("/findByUserId/{id}")
    public BaseResult findByUserId(@PathVariable("id") Integer id){
        List<TbEnterprise> tbEnterpriseList = enterpriseService.findByUserId(id,1);
        return BaseResult.ok("查询成功!",tbEnterpriseList);
    }
    @GetMapping("/findByUserBan/{id}")
    public BaseResult findByUserBan(@PathVariable("id") Integer id){
        List<TbEnterprise> tbEnterpriseList = enterpriseService.findByUserId(id,4);
        return BaseResult.ok("查询成功!",tbEnterpriseList);
    }
    @GetMapping("/findByUserExamine/{id}")
    public BaseResult findByUserExamine(@PathVariable("id") Integer id){
        List<TbEnterprise> tbEnterpriseList = enterpriseService.findByUserId(id,2);
        return BaseResult.ok("查询成功!",tbEnterpriseList);
    }
    @GetMapping("/findByUserExamineFailed/{id}")
    public BaseResult findByUserExamineFailed(@PathVariable("id") Integer id){
        List<TbEnterprise> tbEnterpriseList = enterpriseService.findByUserId(id,3);
        return BaseResult.ok("查询成功!",tbEnterpriseList);
    }
    @GetMapping("/banByUserId/{id}")
    public BaseResult banByUserId(@PathVariable("id") Integer id){
        List<TbEnterprise> tbEnterpriseList = enterpriseService.findByUserId(id,1);
        for (TbEnterprise tbEnterprise : tbEnterpriseList) {
            banEnterprise(tbEnterprise.getId());
        }
        return BaseResult.ok("封禁成功");
    }
    @GetMapping("/findById/{id}")
    public BaseResult<TbEnterprise> findById(@PathVariable("id") Integer id){
        TbEnterprise tbEnterprise = enterpriseService.findById(id);
        return BaseResult.ok("查询成功",tbEnterprise);
    }
    @PutMapping("/updateById")
    public BaseResult updateById(@RequestBody TbEnterprise tbEnterprise){
        tbEnterprise.setUpdateTime(new Date());
        boolean b = enterpriseService.updateById(tbEnterprise);
        if (b){
            return BaseResult.ok("更新成功");
        }else {
            return BaseResult.error("更新失败");
        }
    }
    @PostMapping("/save")
    public BaseResult save(@RequestBody TbEnterprise tbEnterprise){
        tbEnterprise.setState(2);
        tbEnterprise.setCreateTime(new Date());
        boolean b = enterpriseService.save(tbEnterprise);
        if (b){
            return BaseResult.ok("保存成功");
        }else {
            return BaseResult.error("保存失败");
        }
    }
    @DeleteMapping("/del/{id}")
    public BaseResult delById(@PathVariable("id") Integer id){
        boolean b = enterpriseService.removeById(id);
        if (b){
            return BaseResult.ok("删除成功");
        }else {
            return BaseResult.error("删除失败");
        }
    }
    @ApiModelProperty(value = "查询待审核的企业")
    @PostMapping("/findEnterpriseByState")
    public BaseResult findEnterpriseByState(@RequestBody EnterpriseVo enterpriseVo){
        Page list =  enterpriseService.findEnterpriseByState(enterpriseVo);
       return BaseResult.ok("查询成功",list);
    }
    @GetMapping("ban/{id}")
    public BaseResult banEnterprise(@PathVariable("id") Integer id){
        enterpriseService.ban(id);
        TbEnterprise tbEnterprise = enterpriseService.getById(id);
        goEasyPublish.Publish(tbEnterprise.getUserId()+"","您的企业<"+tbEnterprise.getName()+">已被封禁,可在封禁的列表中查看");
        return BaseResult.ok("封禁成功");
    }
    @ApiModelProperty(value = "根据企业id解封")
    @GetMapping("/unBan/{id}")
    public BaseResult unBan(@PathVariable("id") Integer id){
        TbEnterprise  tbEnterprise= enterpriseService.getById(id);
        tbEnterprise.setState(1);
        enterpriseService.updateById(tbEnterprise);
        goEasyPublish.Publish(tbEnterprise.getUserId()+"","您的企业<"+tbEnterprise.getName()+">已解封,可在企业列表中查看");
        return BaseResult.ok("解封成功");
    }
    @ApiModelProperty(value = "根据用户id解封")
    @GetMapping("/unBanByUserId/{id}")
    public BaseResult unBanByUserId(@PathVariable("id") Integer id){
        List<TbEnterprise> tbEnterpriseList = enterpriseService.findByUserId(id,1);
        for (TbEnterprise tbEnterprise : tbEnterpriseList) {
            tbEnterprise.setState(1);
            enterpriseService.updateById(tbEnterprise);
        }
        return BaseResult.ok("解封成功");
    }

    @PostMapping("/condition")
    public BaseResult condition(@RequestBody EnterpriseVo enterpriseVo){
        Page page = enterpriseService.condition(enterpriseVo);
        return BaseResult.ok("查询成功",page);
    }
    @PutMapping("/examine/{id}")
    public BaseResult examine(@PathVariable("id") Integer id){
        TbEnterprise byId = enterpriseService.getById(id);
        byId.setState(1);
        enterpriseService.updateById(byId);
        return BaseResult.ok("审核完成");
    }
    @PutMapping("/failedExamine/{id}")
    public BaseResult failedExamine(@PathVariable("id") Integer id){
        TbEnterprise byId = enterpriseService.getById(id);
        byId.setState(3);
        enterpriseService.updateById(byId);
        goEasyPublish.Publish(byId.getUserId()+"","您的企业<"+byId.getName()+">审核失败了,可在审核失败的列表中查看,修改资料可再次提交审核");
        return BaseResult.ok("审核失败");
    }
    @GetMapping("/findByName")
    public BaseResult findByName(String name){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("name",name);
        List list = enterpriseService.list(queryWrapper);
        return BaseResult.ok("查询成功",list);
    }
    @GetMapping("/findCount")
    public BaseResult<Integer> findCount(){
        Integer size = enterpriseService.list().size();
        return BaseResult.ok("查询成功",size);
    }
}

